"""

Checks module availability and provides meaningful error messages 
if certain packages are missing

$Rev: 197 $ 
$Author: ialbert $ 
$Date: 2008-02-01 10:27:35 -0500 (Fri, 01 Feb 2008) $
"""
import sys
import atlas

# will bail out on error
ERROR_FLAG = False

def empty( text ):
    return text.strip()

#
# testing the existence of required modules
#
required = """

sqlalchemy:incorrect settings, unable to import the sqlalchemy module from library.zip
numpy:numpy module not found, install it from http://numpy.scipy.org/
tables:pytables module not found, install it from http://www.pytables.org/

""".splitlines()

required = filter( empty, required )

for line in required:
    modname, modmsg = line.split(':', 1)
    try:
        __import__(modname)
    except ImportError:
        atlas.error( modmsg )
        ERROR_FLAG = True

try:
    # detect pytables version
    import tables
    curr_vers = tables.__version__
    target_vers = '2.0'
    if  curr_vers < target_vers:
        msg = "pytables module version %s is recommended (version %s was found), download and install it from http://www.pytables.org/" % (target_vers, curr_vers)
        atlas.error( msg )
        ERROR_FLAG = True

except ImportError:
    # this import error has already been caught
    pass

#
# testing the existence of recommended modules
#
recommended = """

pychartdir:pychartdir not found, plotting disabled, get ChartDirector for python from http://www.advsofteng.com/"

""".splitlines()

recommended = filter( empty, recommended )

for line in recommended:
    modname, modmsg = line.split(':', 1)
    try:
        __import__(modname)
    except ImportError:
        atlas.warn( modmsg )

if ERROR_FLAG:
    atlas.error('requirements not met, stopping')
    sys.exit()
